<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验编号" prop="experimentNumber">
        <el-input
          :disabled="isExperimentNumberDisabled"
          v-model="queryParams.experimentNumber"
          placeholder="请输入实验编号"
          clearable
          @keyup.enter.native="handleQuery"
          type="number"
        />
      </el-form-item>
      <el-form-item label="耐摩擦色牢度-干" prop="wearproofColorFastnessDry">
        <el-input
          v-model="queryParams.wearproofColorFastnessDry"
          placeholder="请输入耐摩擦色牢度-干"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="耐摩擦色牢度-湿" prop="wearproofColorFastnessWet">
        <el-input
          v-model="queryParams.wearproofColorFastnessWet"
          placeholder="请输入耐摩擦色牢度-湿"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="耐水洗色牢度-沾色" prop="washableColorFastnessStaining">
        <el-input
          v-model="queryParams.washableColorFastnessStaining"
          placeholder="请输入耐水洗色牢度-沾色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="耐水洗色牢度-变色" prop="washableColorFastnessDiscolor">
        <el-input
          v-model="queryParams.washableColorFastnessDiscolor"
          placeholder="请输入耐水洗色牢度-变色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="深浅度" prop="shade">
        <el-input
          v-model="queryParams.shade"
          placeholder="请输入深浅度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鲜艳度" prop="brilliance">
        <el-input
          v-model="queryParams.brilliance"
          placeholder="请输入鲜艳度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否染匀" prop="whetherDyedEvenly">
        <el-input
          v-model="queryParams.whetherDyedEvenly"
          placeholder="请输入是否染匀"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合格" prop="acceptable">
        <el-select v-model="queryParams.acceptable" placeholder="请选择合格" clearable>
          <el-option
            v-for="dict in dict.type.test_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          :disabled="addDisable"
          v-hasPermi="['ruoyiyinzheng:experimentResult:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyiyinzheng:experimentResult:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyiyinzheng:experimentResult:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyiyinzheng:experimentResult:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="experimentResultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="实验编号" align="center" prop="experimentNumber" />
      <el-table-column label="耐摩擦色牢度-干" align="center" prop="wearproofColorFastnessDry" />
      <el-table-column label="耐摩擦色牢度-湿" align="center" prop="wearproofColorFastnessWet" />
      <el-table-column label="耐水洗色牢度-沾色" align="center" prop="washableColorFastnessStaining" />
      <el-table-column label="耐水洗色牢度-变色" align="center" prop="washableColorFastnessDiscolor" />
      <el-table-column label="深浅度" align="center" prop="shade" />
      <el-table-column label="鲜艳度" align="center" prop="brilliance" />
      <el-table-column label="是否染匀" align="center" prop="whetherDyedEvenly" />
      <el-table-column label="实验报告" align="center" prop="pictureId" v-if="false"/>
      <el-table-column label="测试报告ID" align="center" prop="reportId" v-if="false" />
      <el-table-column label="实验图片" align="center" prop="pictureName" />
      <el-table-column label="检测报告" align="center" prop="reportName" />
      <el-table-column label="合格" align="center" prop="acceptable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.test_result" :value="scope.row.acceptable"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown size="mini" @command="(command) => handleCommandFile(command, scope.row)" v-hasPermi="['ruoyiyinzheng:experimentResult:edit', 'system:oss:download']" >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">文件</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddResult" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownLoadResult" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <el-dropdown size="mini" @command="(command) => handleCommandImage(command, scope.row)" v-hasPermi="['ruoyiyinzheng:experimentResult:edit', 'system:oss:download']" >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">图片</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddResult" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownLoadResult" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyiyinzheng:experimentResult:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyiyinzheng:experimentResult:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实验结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="实验编号" prop="experimentNumber">
          <el-input v-model="form.experimentNumber" placeholder="请输入实验编号"  :disabled="isFormDisabled" type="number"  />
        </el-form-item>
        <el-form-item label="耐摩擦色牢度-干" prop="wearproofColorFastnessDry">
          <el-input v-model="form.wearproofColorFastnessDry" placeholder="请输入耐摩擦色牢度-干" />
        </el-form-item>
        <el-form-item label="耐摩擦色牢度-湿" prop="wearproofColorFastnessWet">
          <el-input v-model="form.wearproofColorFastnessWet" placeholder="请输入耐摩擦色牢度-湿" />
        </el-form-item>
        <el-form-item label="耐水洗色牢度-沾色" prop="washableColorFastnessStaining">
          <el-input v-model="form.washableColorFastnessStaining" placeholder="请输入耐水洗色牢度-沾色" />
        </el-form-item>
        <el-form-item label="耐水洗色牢度-变色" prop="washableColorFastnessDiscolor">
          <el-input v-model="form.washableColorFastnessDiscolor" placeholder="请输入耐水洗色牢度-变色" />
        </el-form-item>
        <el-form-item label="深浅度" prop="shade">
          <el-input v-model="form.shade" placeholder="请输入深浅度" />
        </el-form-item>
        <el-form-item label="鲜艳度" prop="brilliance">
          <el-input v-model="form.brilliance" placeholder="请输入鲜艳度" />
        </el-form-item>
        <el-form-item label="是否染匀" prop="whetherDyedEvenly">
          <el-input v-model="form.whetherDyedEvenly" placeholder="请输入是否染匀" />
        </el-form-item>
<!--        <el-form-item label="实验报告" prop="pictureId">-->
<!--          <el-input v-model="form.pictureId" placeholder="请输入实验报告" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="测试报告" prop="reportId">-->
<!--          <el-input v-model="form.reportId" placeholder="请输入测试报告" />-->
<!--        </el-form-item>-->

        <el-form-item label="合格" prop="acceptable">
          <el-select v-model="form.acceptable" placeholder="请选择合格" clearable>
            <el-option
              v-for="dict in dict.type.test_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openFile" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名">
          <fileUpload v-model="form.file" @fileUploadSuccess="updateFileSuccess" v-if="type === 0"/>
          <imageUpload v-model="form.image" @imageUploadSuccess="updateFileSuccess" v-if="type === 1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExperimentResult, getExperimentResult, delExperimentResult, addExperimentResult, updateExperimentResult} from "@/api/ruoyiyinzheng/experimentResult";

export default {
  name: "ExperimentResult",
  dicts: ['test_result'],
  data() {
    return {
      isFileOrImage:0,
      type:0,
      isExperimentNumberDisabled:false,
      isFormDisabled: false, // 默认值为false，表单项是可以被编辑的
      openFile:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      //新增按钮是否禁用
      addDisable:false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验结果表格数据
      experimentResultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        experimentNumber: undefined,
        wearproofColorFastnessDry: undefined,
        wearproofColorFastnessWet: undefined,
        washableColorFastnessStaining: undefined,
        washableColorFastnessDiscolor: undefined,
        shade: undefined,
        brilliance: undefined,
        whetherDyedEvenly: undefined,
        pictureId: undefined,
        reportId: undefined,
        acceptable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        experimentNumber: [
          { required: true, message: "实验编号不能为空", trigger: "blur" }
        ],
        // reportId: [
        //   { required: true, message: "测试报告不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  // watch: {
  //   '$route.query.experimentNumber': function(newVal) {
  //     console.log(newVal+'更换');
  //     if (newVal!=undefined) {
  //       // 触发查询功能
  //       this.isExperimentNumberDisabled=true;
  //       // this.addDisable=true;
  //       this.queryParams.experimentNumber=newVal;
  //     }
  //     // this.experimentNumber = newVal;
  //     // 执行其他操作...
  //   }
  // },
  activated() {
    this.queryParams.experimentNumber = this.$route.query.experimentNumber;
    this.getList()
  },
  created() {
    const experimentNumber = this.$route.query.experimentNumber;
    if (experimentNumber) {
      // 触发查询功能
      this.isExperimentNumberDisabled=true;
      // this.addDisable=true;
      this.queryParams.experimentNumber=experimentNumber;
      console.log(this.queryParams.experimentNumber);

    }
    else
    {
      // this.addDisable=false;
      this.isExperimentNumberDisabled=false;
    }
    this.getList();
  },
  methods: {

    updateFileSuccess(data)
    {
      if (this.isFileOrImage==1){
        this.form.reportId=data.ossId;
      }
      else {
        this.form.pictureId=data.ossId;
      }

      console.log(data);
    },
    handleCommandFile(command, row)
    {
      this.type=0;
      this.isFileOrImage=1;
      const id = row.id || this.ids
      getExperimentResult(id).then(response => {
        this.form = response.data;
      });
      switch (command) {
        case "handleAddResult":
          this.handleAddResult(row);
          break;
        case "handleDownLoadResult":
          this.handleDownLoadResult(row);
          break;
        default:
          break;
      }
    },
    handleCommandImage(command, row)
    {
      this.type=1;
      this.isFileOrImage=2;
      const id = row.id || this.ids
      getExperimentResult(id).then(response => {
        this.form = response.data;
      });
      switch (command) {
        case "handleAddResult":
          this.handleAddResult(row);
          break;
        case "handleDownLoadResult":
          this.handleDownLoadResult(row);
          break;
        default:
          break;
      }
    },
    // handleCommand(command, row) {
    //   const id = row.id || this.ids
    //   getExperimentResult(id).then(response => {
    //     this.form = response.data;
    //   });
    //   switch (command) {
    //     case "handleAddResult":
    //       this.handleAddResult(row);
    //       break;
    //     case "handleDownLoadResult":
    //       this.handleDownLoadResult(row);
    //       break;
    //     default:
    //       break;
    //   }
    // },
    /** 查询实验结果列表 */
    getList() {
      this.loading = true;
      listExperimentResult(this.queryParams).then(response => {
        console.log(response);
        if (response.total==0){
          this.addDisable=false;
        }
        this.experimentResultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        experimentNumber: undefined,
        wearproofColorFastnessDry: undefined,
        wearproofColorFastnessWet: undefined,
        washableColorFastnessStaining: undefined,
        washableColorFastnessDiscolor: undefined,
        shade: undefined,
        brilliance: undefined,
        whetherDyedEvenly: undefined,
        pictureId: undefined,
        reportId: undefined,
        acceptable: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.form.experimentNumber=this.queryParams.experimentNumber;
      this.open = true;
      this.isFormDisabled=false;
      this.title = "添加实验结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getExperimentResult(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.isFormDisabled=true;
        this.title = "修改实验结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateExperimentResult(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addExperimentResult(this.form).then(response => {
                console.log(response)
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }

        //}
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实验结果编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delExperimentResult(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyiyinzheng/experimentResult/export', {
        ...this.queryParams
      }, `experimentResult_${new Date().getTime()}.xlsx`)
    },
    handleAddResult(row) {
      this.openFile=true;
    },
    handleDownLoadResult(row) {
      if (this.isFileOrImage==1){
        if (row.reportId!=undefined)
        {
          this.$download.oss(row.reportId);
        }
        else
        {
          this.$message({
            message: '检测报告为空，请先上传', // 提示文本内容
            type: 'warning' // 提示类型，这里设置为警告类型
          });
        }
      }
      else {
        if (row.pictureId!=undefined)
        {
          this.$download.oss(row.pictureId);
        }
        else
        {
          this.$message({
            message: '实验图片为空，请先上传', // 提示文本内容
            type: 'warning' // 提示类型，这里设置为警告类型
          });
        }
      }

    }
  }
};
</script>
